package conversion.controller;


import conversion.bean.User;
import conversion.plugin.PluginManager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
public class PageController {

    @Resource
    PluginManager pluginManager;

    @GetMapping(value = {"/","/index"})
    public String index(Model model,
                        HttpSession session){
        Set<String> convertTypes = pluginManager.loadPlugins().keySet();
        List<String[]> convertTypeList = new ArrayList<>();
        for (String convertType : convertTypes) {
            String[] type = convertType.split("to");
            convertTypeList.add(type);
        }
        model.addAttribute("convertTypeList",convertTypeList);
        model.addAttribute("user", session.getAttribute("user"));
        return "index";
    }


    @GetMapping(value = "convert/{convertType}")
    public String convert(@PathVariable("convertType") String convertType,
                          Model model,
                          HttpSession session){
        String[] types = convertType.split("to");

        model.addAttribute("inputType",types[0].toUpperCase());
        model.addAttribute("outputType",types[1].toUpperCase());
        model.addAttribute("user", session.getAttribute("user"));
        return "convert";
    }


    @GetMapping("/login")
    public String login(){
        return "login";
    }

    @GetMapping("/register")
    public String register(){
        return "register";
    }

    @GetMapping("/retrievePassword")
    public String retrievePassword(){
        return "account/retrievePassword";
    }


    @GetMapping("/filemanage")
    public String filemanage(Model model,
                             HttpSession session){
        model.addAttribute("user", session.getAttribute("user"));
        return "filemanage";
    }

    @GetMapping("/pluginmanage")
    public String plugmanage(Model model,
                             HttpSession session){
        model.addAttribute("user", session.getAttribute("user"));
        return "pluginmanage";
    }

    @GetMapping("/formatting")
    public String formatting(Model model,
                             HttpSession session){
        model.addAttribute("user", session.getAttribute("user"));
        return "formatting";
    }


    @GetMapping("/savefile")
    public String savefile(Model model,
                           HttpSession session){
        model.addAttribute("user", session.getAttribute("user"));
        return "savefile";
    }

    @GetMapping("/home")
    public String home(Model model,
            HttpSession session) {
        model.addAttribute("user", session.getAttribute("user"));
        return "account/home";
    }







}
